package lostland.gmud.exv2.data;

import android.os.Bundle;
import android.util.SparseArray;

import com.orhanobut.logger.Logger;

import lostland.gmud.exv2.Game;
import lostland.gmud.exv2.GameEvent;
import lostland.gmud.exv2.Gmud;
import lostland.gmud.exv2.GmudEvent;
import lostland.gmud.exv2.MapScreen;
import lostland.gmud.exv2.battle.BattleScreen;
import lostland.gmud.exv2.battle.proc.holder.DuchangTaskBattle;
import lostland.gmud.exv2.ui.TalkingScreen;
import lostland.gmud.exv2.ui.core.YesNoScreen;

/**
 * 新的事件类，静态用
 * Created by 12548 on 2015/5/6.
 */
public class Event {

	private static final SparseArray<GmudEvent> talkingTriggers = new SparseArray<>();

	public static void talkingTrigger(int npcid, GmudEvent event) {
		if (!__init)
			init();
		talkingTriggers.put(npcid, event);
	}

	public static SparseArray<GmudEvent> getTalkingTriggers() {
		if (!__init)
			init();
		return talkingTriggers;
	}


	private static boolean __init = false;



	/**
	 * 寻找一个可以刷出NPC的地点。
	 *
	 * @return 可用的NPC刷新地点
	 */
	public static FlyPoint findRefreshPlace() {
		int map;
		int ex,ey;
		boolean ok;

		do {
			map = Gmud.rand.nextInt(Gmud.maps.length);
		} while(Gmud.maps[map].extraInfo.BadmanDisabled ||
				map == MapScreen.getInstance().getMap().getId()); //不刷在主角所在地图

		do {
			ok = true;
			ex = (int) (Math.random() * (Gmud.maps[map].width - 2) + 1);
			ey = (int) (Math.random() * (Gmud.maps[map].height - 2) + 1);
			for (int i = ex - 1; i <= ex + 1; i++) {
				for (int j = ey - 1; j <= ey + 1; j++) {
					if (Gmud.maps[map].getEvent(i, j) > 0
							&& Gmud.maps[map].getWalkable(i, j) == GmudMap.MP_CHANGETO) {
						ok = false; // 不刷在出入口周围
					}
				}
			}

			if (!Gmud.maps[map].isWalkable(ex - 1, ey) &&
					!Gmud.maps[map].isWalkable(ex + 1, ey) &&
					!Gmud.maps[map].isWalkable(ex, ey - 1) &&
					!Gmud.maps[map].isWalkable(ex, ey + 1)
					) {
				ok = false; //不刷在四方皆不可通行的位置
			}

			if(Gmud.maps[map].getEvent(ex, ey) > 0 //不刷在有事件的地点
					|| Gmud.maps[map].getWalkable(ex, ey) == GmudMap.MP_UNWALKABLE) {
				ok = false;
			}
		} while (!ok);

		return new FlyPoint(map, ex, ey);
	}

	public static void init() {
		if (__init) return;
		__init = true; // 此两句务必不能挪走

		talkingTrigger(Gmud.findNpc("武器强化师").id, new GmudEvent() {
			@Override
			public boolean execute(Bundle args) {
				GameEvent.callEvent(17);
				return true;
			}
		});

		talkingTrigger(Gmud.findNpc("护甲强化师").id, new GmudEvent() {
			@Override
			public boolean execute(Bundle args) {
				GameEvent.callEvent(18);
				return true;
			}
		});

		talkingTrigger(Gmud.findNpc("潦倒的作者").id, new GmudEvent() {
			@Override
			public boolean execute(Bundle args) {
				GameEvent.callEvent(20);
				return true;
			}
		});

		talkingTrigger(Gmud.findNpc("龙五").id, new GmudEvent() {
			@Override
			public boolean execute(Bundle args) {
				if(Gmud.mc.exp < 20000 || Gmud.getGameDay() < Game.getData().DuchangTaskCooldownDay) {
					return false;
				} else {
					new YesNoScreen("我们赌场最近缺少武功高强的护卫，这位大侠有没有兴趣来兼职？"){
						@Override
						protected void onYes() {
							this.removeFromGame();
							new DuchangTaskBattle().pushToGame();
							new TalkingScreen("你开始在赌场里巡逻。").pushToGame();
						}

						@Override
						protected void onNo() {
							this.removeFromGame();
						}
					}.pushToGame();
					return true;
				}
			}
		});

		talkingTrigger(Gmud.findNpc("捕快").id, new GmudEvent() {
			@Override
			public boolean execute(Bundle args) {
				if (Gmud.mc.fame < 128) {
					BattleScreen.battle(Gmud.findNpc("捕快").id);
					new TalkingScreen("你不是通缉犯吗？来人，给我拿下！").pushToGame();
				} else if (IngameData.getInstance().huntingBadman) {
					new TalkingScreen("在下不是请你去收服" + Gmud.getNpc(IngameData.getInstance().badman).name + "吗？").pushToGame();
				} else if (IngameData.getInstance().nextBadman > 0) {
					new TalkingScreen("多谢，奸邪暂时已经除尽了，请休息一下吧！(下个恶人明天才会出现)").pushToGame();
				} else {

					int badman_start = 184;
					int badman_end = 185;
					int badman_count = badman_end - badman_start + 1;

					IngameData.getInstance().badman = Gmud.rand.nextInt(badman_count) + badman_start;

					IngameData.getInstance().badmanPlace = findRefreshPlace();

					String s1[] = new String[]{"赵", "钱", "孙", "李", "周", "吴", "郑", "王"};
					String s2[][] = new String[][]{{"英", "雄", "豪", "杰"}, {"梅", "兰", "竹", "菊"}};
					Gmud.getNpc(IngameData.getInstance().badman).name = s1[(int) (Math.random() * s1.length)] +
							s2[Gmud.getNpc(IngameData.getInstance().badman).sex][(int) (Math.random() * s2[Gmud.getNpc(IngameData.getInstance().badman).sex].length)];

					Gmud.getNpc(IngameData.getInstance().badman).place(IngameData.getInstance().badmanPlace);

					Gmud.getNpc(IngameData.getInstance().badman).faction = (int) (Math.random() * 8 + 1);
					Gmud.getNpc(IngameData.getInstance().badman).age = Gmud.mc.age + Gmud.rand.nextInt(6);

					new TalkingScreen("近有恶人[" + Gmud.getNpc(IngameData.getInstance().badman).name + "]在" +
							IngameData.getInstance().badmanPlace.getMap().getName() + "为非作歹，请速去为民除害！").pushToGame();

					IngameData.getInstance().huntingBadman = true;
				}
				return true;
			}
		});

		talkingTrigger(Gmud.findNpc("独脚大盗").id, new GmudEvent() {
			@Override
			public boolean execute(Bundle args) {
				if (Gmud.mc.fame == 128) {
					return false;
				} else if (Gmud.mc.fame > 128) {
					new TalkingScreen("哼！老夫今日落入你手，要杀要剐悉听尊便！").pushToGame();
				} else if (IngameData.getInstance().huntingNiceguy) {
					new TalkingScreen("大哥啊，" + Gmud.getNpc(IngameData.getInstance().niceguy).name + "还没死呢，求您一定要帮我！").pushToGame();
				} else if (IngameData.getInstance().nextNiceguy > 0) {
					new TalkingScreen("大恩不言谢！这样一来在明天之前小弟就可以放松了！小弟以后就靠大哥罩着了！").pushToGame();
				} else {

					int niceguy_start = 186;
					int niceguy_end = 187;
					int niceguy_count = niceguy_end - niceguy_start + 1;

					IngameData.getInstance().niceguy = Gmud.rand.nextInt(niceguy_count) + niceguy_start;

					IngameData.getInstance().niceguyPlace = findRefreshPlace();

					Gmud.getNpc(IngameData.getInstance().niceguy).place(IngameData.getInstance().niceguyPlace);

					String s1[] = new String[]{"万俟", "上官", "东方", "尉迟", "公冶", "濮阳", "淳于", "钟离"};
					String s2[][] = new String[][]{{"德", "财", "坚", "备", "轼", "钧"},
							{"奈", "樱", "绪", "花", "春", "秋"}};
					Gmud.getNpc(IngameData.getInstance().niceguy).name = s1[Gmud.rand.nextInt(s1.length)]
							+
							s2[Gmud.getNpc(IngameData.getInstance().niceguy).sex][Gmud.rand.nextInt(s2[Gmud.getNpc(IngameData.getInstance().niceguy).sex].length)];

					Gmud.getNpc(IngameData.getInstance().niceguy).faction = (int) (Math.random() * 8 + 1);
					Gmud.getNpc(IngameData.getInstance().niceguy).age = Gmud.mc.age + Gmud.rand.nextInt(6);

					new TalkingScreen("大哥！小弟正在被[" + Gmud.getNpc(IngameData.getInstance().niceguy).name + "]追杀！如果不去" +
							IngameData.getInstance().niceguyPlace.getMap().getName() + "杀了TA，TA就要杀了我啊！").pushToGame();

					IngameData.getInstance().huntingNiceguy = true;
				}
				return true;
			}
		});

		talkingTrigger(Gmud.findNpc("郭襄").id, new GmudEvent() {
			@Override
			public boolean execute(Bundle args) {
				if(Gmud.mc.have(Gmud.findItem("郭襄的信").id))
					return false;
				else {
					new TalkingScreen("等我找到杨过大哥哥之后我一定回家，我这里有一封信你先帮我转交给我家人吧。").pushToGame();
					Gmud.mc.force_give(Gmud.findItem("郭襄的信").id);

					int npcMap = args.getInt("npcMap");
					int npcX = args.getInt("npcX");
					int npcY = args.getInt("npcY");
					Logger.i("参数已获取： npcMap = %d, npcX = %d, npcY = %d", npcMap, npcX, npcY);

					Gmud.maps[npcMap].setEvent(npcX, npcY, -1);
					Gmud.maps[npcMap].setWalkable(npcX, npcY, GmudMap.MP_WALKABLE);

					guoxiang = false;
					return true;
				}
			}
		});

		talkingTrigger(Gmud.findNpc("黄蓉").id, new GmudEvent() {
			@Override
			public boolean execute(Bundle args) {
				if(!Gmud.mc.have(Gmud.findItem("郭襄的信").id))
					return false;
				else {

					Gmud.mc.drop(Gmud.findItem("郭襄的信").id, 1);

					int goldReword = Gmud.rand.nextInt(101) + 100;
					int potReword = Gmud.rand.nextInt(201) + 80 * Gmud.mc.getpj();

					new TalkingScreen(String.format("你被奖励了： %d金钱， %d潜能", goldReword, potReword)).pushToGame();
					new TalkingScreen("啊，这是我女儿的字……").pushToGame();

					Gmud.mc.earnMoney(goldReword);
					Gmud.mc.setPotential(Gmud.mc.potential + potReword);

					refreshGuoxiang();

					return true;
				}
			}
		});


	}

	public static boolean guoxiang = false;
	public static void refreshGuoxiang() {
		if(guoxiang) return;
		final FlyPoint place = findRefreshPlace();
		Logger.i("郭襄刷新于%s的（%d,%d）", place.getMap().getName(), place.x, place.y);
		Gmud.findNpc("郭襄").place(place);
		guoxiang = true;
	}
}
